package pactera.tf.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.extern.slf4j.Slf4j;
import pactera.tf.dataobj.ChatFriendsDO;
import pactera.tf.dataobj.LoginDO;
import pactera.tf.dataobj.UserinfoDO;
import pactera.tf.excpetion.R;
import pactera.tf.mapper.LoginMapper;
import pactera.tf.service.ChatFriendsService;
import pactera.tf.service.LoginService;
import pactera.tf.service.UserinfoService;
import pactera.tf.utils.Md5Util;


@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private ChatFriendsService chatFriendsService;


    @Override
    public LoginDO getLoginByUsername(String username) {
        return loginMapper.getLoginByUsername(username);
    }

    @Override
    public String lkUseridByUsername(String username) {
        return loginMapper.lkUseridByUsername(username);
    }

    @Override
    public Map<String,Object> checkLogin(LoginDO loginDO) {
        LoginDO login = loginMapper.getLoginByUsername(loginDO.getUsername());
        Map<String,Object> map = new HashMap<>(2);
        if(login == null){
            map.put("R", R.error().message("用户名不存在"));
            return map;
        }

        //判断密码
        if(!login.getUserpassword().equals(Md5Util.stringInMd5(loginDO.getUserpassword()))){
            map.put("R", R.error().message("密码错误"));
            return map;
        }
        map.put("logininfo",login);
        map.put("R", R.ok().message("登录成功"));
        return map;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean register(LoginDO loginDO) {
        LoginDO login = loginMapper.getLoginByUsername(loginDO.getUsername());
        if(login != null){
            log.info("用户{}已经存在",login.getUsername());
            return false;
        }
        //生成id
        loginDO.setUserid(System.currentTimeMillis()+"");
        loginDO.setUserpassword(Md5Util.stringInMd5(loginDO.getUserpassword()));
        int result = loginMapper.insertSelective(loginDO);
        if(result == 1){
            //创建userinfo
            UserinfoDO user = new UserinfoDO();
            user.setNickname(loginDO.getUsername());
            user.setUserid(loginDO.getUserid());
            Random random = new Random();
            user.setUimg("/img/"+random.nextInt(10)+".jpg");
            userinfoService.insertSelective(user);

            ChatFriendsDO chatFriendsDO = new ChatFriendsDO();
            chatFriendsDO.setUserid("1");
            chatFriendsDO.setFuserid(loginDO.getUserid());
            chatFriendsService.insertUserFriend(chatFriendsDO);
            return true;
        }

        return false;
    }

    @Override
    public LoginDO getLoginByUserid(String userid) {
        return loginMapper.getLoginByUserid(userid);
    }
}
